package com.itheima.mall.service.pms;

import cn.hutool.core.date.DateUtil;
import com.itheima.mall.api.pms.ProductServiceApi;
import com.itheima.mall.api.pms.ProductVertifyRecordServiceApi;
import com.itheima.mall.interceptor.AdminHolder;
import com.itheima.mall.pojo.pms.Product;
import com.itheima.mall.pojo.pms.ProductVertifyRecord;
import com.itheima.mall.pojo.ums.Admin;
import com.itheima.mall.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductService {
    @DubboReference
    private ProductServiceApi productServiceApi;
    @DubboReference
    private ProductVertifyRecordServiceApi productVertifyRecordServiceApi;
    //创建商品
    public void create(Product product) {
        productServiceApi.create(product);
    }

    public PageResult list(Integer brandId, String keyword, Integer pageNum, Integer pageSize,
                           Integer productCategoryId, String productSn, Integer publishStatus, Integer verifyStatus) {
        PageResult pageResult = productServiceApi.list(brandId,keyword,pageNum,pageSize,productCategoryId,
                productSn,publishStatus,verifyStatus);
        return pageResult;
    }

    /**
     * 批量修改删除状态,批量设为新品,批量上下架商品,批量推荐商品
     * @param ids
     * @param deleteStatus
     * @return
     */
    public void updateStatus(Long[] ids, Integer deleteStatus) {
        for (Long id : ids) {
            //根据id查找商品
            Product product = productServiceApi.findProductById(id);
            product.setDeleteStatus(deleteStatus);
            productServiceApi.updateDeleteStatus(product);
        }
    }

    /**
     * 批量修改审核状态
     * @param ids
     * @param verifyStatus
     * @return
     */
    public void updateVerifyStatusStatus(Long[] ids, Integer verifyStatus, String detail) {
        Admin admin = AdminHolder.getAdmin();
        for (Long id : ids) {
            //根据id查找商品
            Product product = productServiceApi.findProductById(id);
            product.setDeleteStatus(verifyStatus);
            productServiceApi.updateDeleteStatus(product);
            //将修改记录保存到数据库
            ProductVertifyRecord productVertifyRecord = new ProductVertifyRecord();
            productVertifyRecord.setProductId(id);
            productVertifyRecord.setStatus(verifyStatus);
            productVertifyRecord.setCreateTime(DateUtil.date());
            productVertifyRecord.setVertifyMan(admin.getUsername());
            productVertifyRecord.setDetail(detail);
            productVertifyRecordServiceApi.save(productVertifyRecord);
        }
    }

    /**
     * 更新商品
     * @param id
     * @param product
     * @return
     */
    public void updateById(Long id, Product product) {
        product.setId(id);
        productServiceApi.updateById(product);
    }

    /**
     * 根据商品id获取商品编辑信息
     * @param id
     * @return
     */
    public Product updateInfoById(Long id) {
        return productServiceApi.updateInfoById(id);
    }

    /**
     * 根据商品名称或货号模糊查询
     * @param keyword
     * @return
     */
    public List<Product> simpleList(String keyword) {
        List<Product> productList = productServiceApi.simpleList(keyword);
        return productList;
    }
}
